package com.tuotu.gps;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.os.Bundle;
import android.os.IBinder;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.tuotu.gps.entity.LocationInfo;
import com.tuotu.gps.entity.MessageEvent;
import com.tuotu.gps.fragment.BaseFragment;
import com.tuotu.gps.fragment.UPFailFragment;
import com.tuotu.gps.fragment.UPSuccessFragment;
import com.tuotu.gps.fragment.UploadinglFragment;
import com.tuotu.gps.utils.CollectionFile;
import com.tuotu.gps.utils.LogUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Vector;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UploadActivity2 extends AppCompatActivity implements UploadService.OnResultListener {

    private ArrayList<Fragment> fragments;

    @BindView(R.id.viewpager)
    ViewPager viewPager;

    UploadService uploadService;

    @BindViews({R.id.title0,R.id.title1,R.id.title2})
    TextView[] titles;

    private Vector<LocationInfo> allVectors;
//    private Vector<LocationInfo> successVectors=new Vector<>();
//    private Vector<LocationInfo> failVectors=new Vector<>();
    private OnQueryResultListener onQueryResultListener;
    private OnSuccessResultListener onSuccessResultListener;
    private OnFailResultListener onFailResultListener;
    ServiceConnection serviceConnection;
    boolean needUpload=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload2);
        needUpload=getIntent().getBooleanExtra("needUpload",true);
        ButterKnife.bind(this);
        init();
        Intent intent=new Intent(this,UploadService.class);
        startService(intent);
        serviceConnection=new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                Log.e("xyww","UploadService Service连接成功");
                uploadService = ((UploadService.MyBinder) service).getService();
                uploadService.setOnResultListener(UploadActivity2.this);
                uploadService.queryUploadFile(RecordActivity2.GPS_DIR,needUpload);
                // 执行Service内部自己的方法
//                myService.excute();
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };
        bindService(intent,serviceConnection , Context.BIND_AUTO_CREATE);
    }

    public void UploadFailFile(String path){
        if(null!=uploadService){
            uploadService.queryUploadFile(path,true);
        }
    }

    public UploadService getUploadService() {
        return uploadService;
    }

    public void setTitles(int position ,int count){
        if(position==0){
            titles[0].setText("正在上传("+count+")");
        }else if(position==1){
            titles[1].setText("上传成功("+count+")");
        }else if(position==2){
            titles[2].setText("上传失败("+count+")");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(serviceConnection);
        Log.e("xyww","UploadActivity2 onDestroy");
    }

    @Override
    public void onBackPressed() {
//        super.onBackPressed();
        Intent intent=new Intent(this,RecordActivity2.class);
        intent.putExtra("needResume",true);
        startActivity(intent);
        finish();
    }

    public interface OnQueryResultListener{
        void onActivityQuery(Vector<LocationInfo> vectors);
        void onActivitySuccess(LocationInfo locationInfo);
        void onActivityFail(LocationInfo locationInfo);
    }
    public interface OnSuccessResultListener{
        void onActivitySuccess(LocationInfo locationInfo);
    }
    public interface OnFailResultListener{
        void onActivityFail(LocationInfo locationInfo);
    }

    public void setOnQueryResultListener(OnQueryResultListener onQueryResultListener) {
        this.onQueryResultListener = onQueryResultListener;
    }

    public void setOnFailResultListener(OnFailResultListener onFailResultListener) {
        this.onFailResultListener = onFailResultListener;
    }

    public void setOnSuccessResultListener(OnSuccessResultListener onSuccessResultListener) {
        this.onSuccessResultListener = onSuccessResultListener;
    }

    private void init() {
        fragments = new ArrayList<Fragment>();
        fragments.add(new UploadinglFragment());
        fragments.add(new UPSuccessFragment());
        fragments.add(new UPFailFragment());
        MyFragmentAdapter adapter= new MyFragmentAdapter(getSupportFragmentManager());
        viewPager.setOffscreenPageLimit(2);
        viewPager.setAdapter(adapter);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                titles[0].setSelected(position==0?true:false);
                titles[1].setSelected(position==1?true:false);
                titles[2].setSelected(position==2?true:false);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        viewPager.setCurrentItem(0);
        titles[0].setSelected(true);
    }

    @OnClick({R.id.title0,R.id.title1,R.id.title2})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.title0:
                viewPager.setCurrentItem(0);
                break;
            case R.id.title1:
                viewPager.setCurrentItem(1);
                break;
            case R.id.title2:
                viewPager.setCurrentItem(2);
                break;
        }
    }

    public Vector<LocationInfo> getAllVectors() {
        return allVectors;
    }

    @Override
    public void onQueryResult(Vector<LocationInfo> vectors) {
        Log.e("xyww","UploadActivity2 onQueryResult");
        allVectors=vectors;
        if(null!=onQueryResultListener){
            onQueryResultListener.onActivityQuery(vectors);
        }
    }

    @Override
    public void onSuccessResult(LocationInfo locationInfo) {
//        Log.e("xyww","UploadActivity2 onSuccessResult");
        allVectors.remove(locationInfo);
//        successVectors.add(locationInfo);
        if(null!=onQueryResultListener)
            onQueryResultListener.onActivitySuccess(locationInfo);
        if(null!=onSuccessResultListener)
            onSuccessResultListener.onActivitySuccess(locationInfo);
        isUploadDone();
    }

    @Override
    public void onFailResult(LocationInfo locationInfo) {
//        Log.e("xyww","UploadActivity2 onFailResult");
        allVectors.remove(locationInfo);
//        failVectors.add(locationInfo);
        if(null!=onQueryResultListener)
            onQueryResultListener.onActivityFail(locationInfo);
        if(null!=onSuccessResultListener)
            onFailResultListener.onActivityFail(locationInfo);
        isUploadDone();
    }

    private void isUploadDone(){
        if(null!=uploadService){
            if(uploadService.getVectors().size()==0)
                UploadService.isUploading=false;
        }else {
            UploadService.isUploading=false;
        }
    }

    private class MyFragmentAdapter extends FragmentPagerAdapter {

        public MyFragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return null;
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return titles.length;
        }
    }
}
